package cn.com.zero.file.service.impl;

import cn.com.zero.file.client.MinioTemplate;
import cn.com.zero.file.config.properties.FileUploadProperties;
import cn.com.zero.file.service.IFileService;
import cn.com.zero.file.util.FileUtil;
import cn.com.zero.file.vo.FileInfo;
import cn.hutool.core.codec.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;

/**
 * @author liuxh
 * @version 1.0.0
 * @Description Minio文件服务实现类
 * @createTime 2022/9/14
 */
public class MinioFileServiceImpl implements IFileService {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private MinioTemplate minioTemplate;
    private FileUploadProperties fileUploadProperties;

    public MinioFileServiceImpl(MinioTemplate minioTemplate) {
        this.minioTemplate = minioTemplate;
        this.fileUploadProperties = minioTemplate.getFileUploadProperties();
    }

    @Override
    public FileInfo upload(MultipartFile file, String relativePath) {
        return upload(file, MinioTemplate.BUCKET_DEFAULT, relativePath);
    }

    @Override
    public FileInfo upload(MultipartFile file, String bucketName, String relativePath) {
        FileInfo fileInfo = FileUtil.getFileInfo(file, relativePath);
        minioTemplate.upload(bucketName, fileInfo.getRelativeFilePath(), file);
        return fileInfo;
    }

    @Override
    public FileInfo uploadImage(MultipartFile file, String bucketName, String relativePath, Integer width, Integer height) {
        String extension = FileUtil.getExtension(file);
        if (!FileUtil.isAllowedExtension(extension, fileUploadProperties.getImageExtension())) {
            log.error("上传图片失败，不允许上传文件类型为[{}]的文件！", extension);
            throw new RuntimeException("上传图片失败，不允许上传文件类型为[" + extension + "]的文件！");
        }
        MultipartFile multipartFile = FileUtil.compressImg(file, width, height);
        return upload(multipartFile, bucketName, relativePath);
    }


    @Override
    public void delete(String relativeFilePath) {
        minioTemplate.deleteObject(relativeFilePath);
    }

    @Override
    public void delete(String bucketName, String relativeFilePath) {
        minioTemplate.deleteObject(bucketName, relativeFilePath);
    }

    @Override
    public Boolean isExist(String relativeFilePath) {
        return minioTemplate.ObjectExist(relativeFilePath);
    }

    @Override
    public Boolean isExist(String bucketName, String relativeFilePath) {
        return minioTemplate.ObjectExist(bucketName, relativeFilePath);
    }

    @Override
    public Resource downloadFile(String bucketName, String relativeFilePath) {
        if (!isExist(bucketName, relativeFilePath)) {
            throw new RuntimeException("存储桶[" + bucketName + "]的文件[" + relativeFilePath + "]不存在，下载失败！");
        }
        byte[] file = minioTemplate.download(bucketName, relativeFilePath);
        return new InputStreamResource(new ByteArrayInputStream(file));
    }

    @Override
    public String downloadFileToBase64(String bucketName, String relativeFilePath) {
        byte[] file = minioTemplate.download(bucketName, relativeFilePath);
        return Base64.encode(file);
    }
}
